<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            margin: 200px;
        }
        
        span {
            display: inline-block;
            width: 40px;
            height: 40px;
            background-color: #333;
            font-size: 20px;
            color: #fff;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>
<body>
    <button>开启倒计时</button>
    <button>关闭倒计时</button>
    <div>距离开学还有：</div>
    <div>
        <span class="day"></span>
        <span class="hour"></span>
        <span class="minute"></span>
        <span class="second"></span>
    </div>
    <script>
        var btn = document.querySelectorAll('button')
        var countDown = null; 
        btn[0].addEventListener('click', function() {

            var day = document.querySelector('.day');
            var hour = document.querySelector('.hour');
            var minute = document.querySelector('.minute');
            var second = document.querySelector('.second');
            var finalTime = +new Date('2022-9-13 08:00:00');

            function leftTime() {
                var nowTime = +new Date();
                var times = (finalTime - nowTime)/1000;
                var d = parseInt(times/60/60/24);
                d = d < 10 ? '0'+d : d;
                day.innerHTML = d;
                var h = parseInt(times/60/60%24);
                h = h < 10 ? '0'+h : h;
                hour.innerHTML = h;
                var m = parseInt(times/60%60);
                m = m < 10 ? '0'+m : m;
                minute.innerHTML = m;
                var s = parseInt(times%60);
                s = s < 10 ? '0'+s : s;
                second.innerHTML = s;
            }

            leftTime();
            countDown = setInterval(leftTime, 1000);
        })
        btn[1].addEventListener('click',function() {
            clearInterval(countDown);
        })
    </script>
    
</body>
</html>